$(document).ready(function() {

	$(document).on('click', '.userInfo', function() {
		var userid = $(this).attr("value");
		console.log(userid);
		window.location.href = "other_homepage.html?userid=" + userid;
	});
})

//搜索
function searchByKeyword() {
	var keyWord = $("#search").val();
	window.location.href = 'search_result.html?keyWord='
		+ keyWord;
}

function loadResult(keyWord) {
	$
			.ajax({
				url : 'search/searchByKeyWord',
				type : "GET",
				data : {
					keyWord : encodeURI(keyWord)
				},
				success : function(data) {
					console.log(data)
					switch (data['status']) {
					case -250:
						window.location.href = "login.html";
						break;
					case 0:
						if (data['result']['hasPosts'] == false) {
							document.getElementById("postsResult").innerHTML = "<img src=\"img/找不到结果.jpg\">";
						} else {
							var posts = data['result']['posts'];
							var postHtml = "";
							for (var i = 0; i < posts.length; i++) {

								postHtml = postHtml
										+ "<div class=\"container\">"
										+ "<div class=\"firstRow\">"
										+ "<a href=\"\">"
										+ "<img src=\""
										+ posts[i]['publishUser']['headImg']
										+ "\" class=\"publishUserImage\" alt=\"\" width=\"30\" height=\"30\" >"
										+ "<a class=\"publishUserName\">"
										+ posts[i]['publishUser']['userName']
										+ "</a>" + "<p class=\"postDate\">"
										+ posts[i]['post']['time'] + "</p>"
										+ "</div>"

										+ "<div class=\"passage\">"
										+ "<a href=\"inner_post.html?postid="
										+ posts[i]['post']['postid']
										+ "\" class=\"passageName\">"
										+ posts[i]['post']['title'] + "</a>"
										+ "<a href=\"inner_post.html?postid="
										+ posts[i]['post']['postid'] + "\">"
										+ "<p class=\"passageMain\">"
										+ posts[i]['post']['txtContent']
										+ "</p>" + "</a>"
										// <div class="media-box">
										// <%
										// for (int x =
										// 0;x<imgList.size()&&x<3;x++){
										// %>
										// <img class="image1"
										// src="<%=imgList.get(x)%>"
										// height="110px" width="110px">
										// <%}%>
										// </div>
										+ "</div>" + "<div class=\"btnCol\">";
								if (posts[i]['isAgreed'] == false) {
									postHtml = postHtml
											+ "<button onclick=\"mylike(this)\" class=\"zan\" id=\"like+<%=list.get(i).getPostID()%>\">赞</button>";
								} else {
									postHtml = postHtml
											+ "<button onclick=\"mylike(this)\" class=\"zan\" id=\"like+<%=list.get(i).getPostID()%>\">已赞</button>";
								}

								postHtml = postHtml
										+ "<button class=\"zanNum\">"
										+ posts[i]['agreeNum'] + "人已赞</button>";
								if (posts[i]['isCollected'] == false) {
									postHtml = postHtml
											+ "<button onclick=\"mycollect(this)\" class=\"collect\" id=\"collect+<%=list.get(i).getPostID()%>\">收藏</button>";
								} else {
									postHtml = postHtml
											+ "<button onclick=\"mycollect(this)\" class=\"collect\" id=\"collect+<%=list.get(i).getPostID()%>\">已收藏</button>";
								}

								postHtml = postHtml + "</div>"

								+ "</div>";
							}
							document.getElementById("postsResult").innerHTML = postHtml;
						}

						if (data['result']['hasUsers'] == false) {
							document.getElementById("usersResult").innerHTML = "<img src=\"img/找不到用户.jpg\">";
						} else {
							var users = data['result']['users'];
							var userHtml = "";
							for (var j = 0; j < users.length; j++) {
								userHtml = userHtml
										+ "<div class=\"userInfo\" style='cursor: pointer;' value= "
										+ users[j]['userid']
										+ ">"
										+ "<div class=\"image\">"

										+ "<img src=\""
										+ users[j]['headImg']
										+ "\" class=\"userImage\" alt=\"\" width=\"50\" height=\"50\" >"
										+ "</div>" + "<div class=\"userText\">"
										+ "<b>" + users[j]['userName'] + "</b>"
										+ "    <p class=\"signature\">"
										+ users[j]['description'] + "</p>"
										+ "</div>" + "</div>"
							}
							document.getElementById("usersResult").innerHTML = userHtml;

						}
						break;
					}
				},
				error : function(err) {
					console.log(err);
				}
			});
}
